//  ========== 
//  = 自动投标设置JS = 
//  ==========
var autoParam = {
	memberId:storage.memberId
};
var isOpen = false;
var Main = function() {
	mui.init({
		swipeBack: false //启用右滑关闭功能
	});
	this.init();
	//this.even();
	this.loadData();
};
Main.prototype = {
	init: function() {
		mui('.mui-scroll-wrapper').scroll({
			indicators: true //是否显示滚动条
		});
	},
	even: function() {
		var _this = this;
		document.querySelector(".mui-switch").addEventListener('toggle', function(event) {
	        isOpen=!isOpen;
	        if(isOpen){//开启
	        	var reqUrl = server_url+"/creditFlow/financingAgency/autoBidPlBidPlan.do?isMobile=1&memberId="+Util.getLoginUser().memberId;
	        	reqUrl += "&rtnType=json";
	        	$.getJSON(reqUrl,{},
	        	function(data){
	        		data = JSON.parse(data);
	        		if(data.success){
	        			var url = server_url+'/user/openBidAutoBpCustMember.do?whetherOpen=1&retUrl=user/automaticbidBpCustMember.do';
	        			url+="&isMobile=1&memberId="+Util.getLoginUser().memberId;
	        			url += "&rtnType=json";
                        $.getJSON(url,{},function(r){
                        	if(r.success && r.message){
                        		mui.toast(r.message);
                        	}
                        });
	        		}else{
	        			mui.toast(data.msg);
	        		}
	        	});
	        }else{//关闭
	        	var url = server_url+'/user/closeBidAutoBpCustMember.do?whetherOpen=1';
	        	url+="&isMobile=1&memberId="+Util.getLoginUser().memberId;
	        	url += "&rtnType=json";
	        	$.getJSON(url,{},function(r){
                        	if(r.success && r.message){
                        		mui.toast(r.message);
                        	}
                });
	        }
	        
	    });
    
		document.querySelector('.l-bid-money').addEventListener('tap',function(){
			mui.prompt("单次投标金额", "请输入单次投标金额",'',["确定", "取消"],function(e) {
					if (e.value.trim() == ''||isNaN(e.value) || e.value<200 || e.value%200 != 0) {
						mui.toast("每次投标金额必须大于200元，且必须能整除200！");
					} else {
						 autoParam.bidMoney = e.value;	
						_this.update(function(){
							$('.m-bid-money').html(e.value+'元');
						});
					}
				});
		});
		//开启自动投标
		//document.querySelector('.l-auto-bid').addEventListener('tap',function(){
		//	
		//});
	  //信用等级范围
		document.querySelector('.l-rate').addEventListener('tap',function(){
			//级联示例
			var picker = new mui.PopPicker({
				layer: 2
			});
			picker.setData(rateData);	
			picker.show(function (selectItems) {  
				autoParam.rateStart = selectItems[0].value;
				autoParam.rateEnd = selectItems[1].value;
				_this.update(function(){
					$('.m-rate').html(Util.safeString(Util.creditRate[autoParam.rateStart],'HA')+' - '+Util.safeString(Util.creditRate[autoParam.rateEnd],'AA'));
				});
			});
			//设置选中项
			_this._pickerSelected(picker,rateData,autoParam.rateStart,autoParam.rateEnd)
		});
		
		//借款期限
		document.querySelector('.l-period').addEventListener('tap',function(){
			//级联示例
			var picker = new mui.PopPicker({
				layer: 2
			});
			picker.setData(periodData);	
			picker.show(function (selectItems) {  
				autoParam.periodStart = selectItems[0].value;
				autoParam.periodEnd = selectItems[1].value;
				_this.update(function(){
					$('.m-period').html(autoParam.periodStart+'月 - '+autoParam.periodEnd+'月');
				});
			 })
			//设置选中项
			_this._pickerSelected(picker,periodData,autoParam.periodStart,autoParam.periodEnd);
		});
		
		//利率范围
		document.querySelector('.l-invest-scope').addEventListener('tap',function(){
			//级联示例
			var picker = new mui.PopPicker({
				layer: 2
			});
			picker.setData(investData);	
			picker.show(function (selectItems) {  
				autoParam.interestStart = selectItems[0].value;
				autoParam.interestEnd= selectItems[1].value;
				_this.update(function(){
					$('.m-invest-scope').html(autoParam.interestStart+'% - '+autoParam.interestEnd+'%');
				});
			})
			//设置选中项
			_this._pickerSelected(picker,investData,autoParam.interestStart,autoParam.interestEnd);
		});
		
		document.querySelector('.l-keep-money').addEventListener('tap',function(){
			mui.prompt("账户保留金额", "请输入账户保留金额",'',["确定", "取消"],function(e) {
					if (e.value.trim() == '') {
						mui.toast("请输入账户保留金额");
					} else {
						if(isNaN(e.value)){
						 mui.toast("账户保留金额请输入整数");	
						}else{
						 autoParam.keepMoney = e.value;	
						_this.update(function(){
							$('.m-keep-money').html(e.value+'元');
						});
						}
					}
				});
		});
	},
	_pickerSelected : function(picker,data,value1,value2){
		for(var i=0;i<data.length;i++){
			if(data[i].value == value1){
				//设置选中项
			    picker.pickers[0].setSelectedIndex(i);
			    for(var j=0;j<data[i].children.length;j++){
			    	if(data[i].children[j].value == value2){
			          picker.pickers[1].setSelectedIndex(j);
			          return ;
			    	}
			    }
			    return ;
			}
		}
		
	},
	update:function(callback) {
		$.post(server_url + '/user/saveAutoBidInfoBpCustMember.do', autoParam, function(result) {
			result = JSON.parse(result);
			if(result.success){
			  mui.toast('修改成功！');	
			}
			if(callback&&$.isFunction(callback)){
				callback();
			}
		});
	},
	loadData: function() {
		var _this = this;
		var url = server_url +'/creditFlow/financingAgency/automaticbidPlBidAuto.do';
		var param = {
			'isMobile':1,
			'memberId':storage.memberId
		}
		$.getJSON(url, param, function(retData) {
			if(retData.success) {
				var item = retData.data;
				$('.m-bid-money').html(item.bidMoney+'元');
				$('.m-invest-scope').html(item.interestStart+'% - '+item.interestEnd+'%');
				$('.m-period').html(item.periodStart+'月 - '+item.periodEnd+'月');
				$('.m-rate').html(Util.safeString(Util.creditRate[item.rateStart],'HA')+' - '+Util.safeString(Util.creditRate[item.rateEnd],'AA'));
				$('.m-keep-money').html(item.keepMoney+'元');
				autoParam.bidMoney = item.bidMoney;
				autoParam.interestStart = item.interestStart;
				autoParam.interestEnd = item.interestEnd;
				autoParam.periodStart = item.periodStart;
				autoParam.periodEnd = item.periodEnd;
				autoParam.rateStart = item.rateStart;
				autoParam.rateEnd = item.rateEnd;
				autoParam.keepMoney = item.keepMoney;
				autoParam.isOpen = item.isOpen;
				isOpen = ('1' == item.isOpen);
				if(isOpen){
					$('.mui-switch').addClass('mui-active');
				}
			}
		});

		//账户余额
		url = server_url + '/user/mobileaccountBpCustMember.do';
		param = {
			isMobile: '1',
			memberId: Util.getLoginUser().memberId
		}
		$.getJSON(url, param, function(retData) {
			if(retData.success) {
				balance = retData.money;
				$('.m-available-money').html(retData.money + '元');
				
				if(retData.money > 50){
					_this.even();
				}
			}
		});

	}
};